package com.ssy.lingxi.order.serviceimpl.web;

import com.ssy.lingxi.common.constant.order.OrderTypeEnum;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.order.entity.OrderDO;
import com.ssy.lingxi.order.model.constant.BuyerInnerStatusEnum;
import com.ssy.lingxi.order.model.constant.OrderOuterStatusEnum;
import com.ssy.lingxi.order.model.constant.OrderServiceContants;
import com.ssy.lingxi.order.model.vo.basic.request.OrderPageVO;
import com.ssy.lingxi.order.model.vo.buyer.response.BuyerOrderQueryVO;
import com.ssy.lingxi.order.model.vo.buyer.response.TransitionOrderQueryVO;
import com.ssy.lingxi.order.service.base.IBaseCacheService;
import com.ssy.lingxi.order.service.base.IBaseOrderConsigneeService;
import com.ssy.lingxi.order.service.base.IBaseOrderService;
import com.ssy.lingxi.order.service.web.IOrderTransitionService;
import com.ssy.lingxi.order.utils.NumberUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.stream.Collectors;

/**
 * 订单转单相关接口实现类
 * @author xiechenhui
 * @version 2.0.0
 * @date 2022-03-04
 */
@Service
public class OrderTransitionServiceImpl implements IOrderTransitionService {

    private final static Logger logger = LoggerFactory.getLogger(OrderTransitionServiceImpl.class);

    @Resource
    private IBaseCacheService baseCacheService;

    @Resource
    private IBaseOrderService baseOrderService;

    @Resource
    private IBaseOrderConsigneeService baseOrderConsigneeService;

    /**
     * 查询订单转单列表
     * @param headers   请求头参数
     * @param pageVO    查询参数
     * @return
     */
    @Override
    public Wrapper<PageData<TransitionOrderQueryVO>> getTransitionOrderListPage(HttpHeaders headers, OrderPageVO pageVO) {
        UserLoginCacheDTO loginUser = baseCacheService.needLoginFromBusinessPlatform(headers);
        Page<OrderDO> orderDOPage = baseOrderService.pageTransitionOrderList(loginUser.getMemberId(), loginUser.getMemberRoleId(), pageVO.getOrderNo(), pageVO.getDigest(), pageVO.getMemberName(), pageVO.getStartDate(), pageVO.getEndDate(), pageVO.getOrderTransitionType(), pageVO.getCurrent(), pageVO.getPageSize());
        return Wrapper.success(new PageData<>(orderDOPage.getTotalElements(), orderDOPage.getContent().stream().map(order -> {
            TransitionOrderQueryVO queryVO = new TransitionOrderQueryVO();
            queryVO.setOrderId(order.getId());
            queryVO.setOrderNo(order.getOrderNo());
            queryVO.setCreateTime(order.getCreateTime().format(OrderServiceContants.DEFAULT_TIME_FORMATTER));
            queryVO.setDigest(order.getDigest());
            queryVO.setBuyerMemberName(order.getBuyerMemberName());
            queryVO.setAmount(NumberUtil.formatAmount(order.getTotalAmount()));
            queryVO.setOrderType(order.getOrderType());
            queryVO.setOrderTypeName(OrderTypeEnum.getNameByCode(order.getOrderType()));
            queryVO.setDeliverAddress(baseOrderConsigneeService.orderConsigneeToString(order.getConsignee()));
            queryVO.setInnerStatus(order.getBuyerInnerStatus());
            queryVO.setInnerStatusName(BuyerInnerStatusEnum.getNameByCode(order.getBuyerInnerStatus()));
            queryVO.setOuterStatus(order.getOuterStatus());
            queryVO.setOuterStatusName(OrderOuterStatusEnum.getNameByCode(order.getOuterStatus()));
            return queryVO;
        }).collect(Collectors.toList())));
    }
}
